package com.cloud.upms.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.core.constant.CommonConstants;
import com.cloud.common.core.exception.ProjectException;
import com.cloud.api.upms.domain.role.RoleAddUpdateReq;
import com.cloud.api.upms.domain.role.RoleListReq;
import com.cloud.api.upms.enums.RoleExceptionEnum;
import com.cloud.api.upms.model.SysRole;
import com.cloud.api.upms.model.SysRoleMenu;
import com.cloud.upms.dao.SysRoleMapper;
import com.cloud.upms.dao.SysRoleMenuMapper;
import com.cloud.upms.service.SysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author:  xuanning
 * Date:    2022/10/29 2:19
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;
    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    @Transactional
    public void addOrUpdate(RoleAddUpdateReq paramReq) {
        String roleName=paramReq.getRoleName();
        String roleCode=paramReq.getRoleCode();
        Long id=paramReq.getId();
        List<Long> menuIds=paramReq.getMenuIds();

        Long nameCount=sysRoleMapper.selectCount(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleName,roleName)
                .eq(SysRole::getDelFlag, CommonConstants.ZERO)
                .ne(id!=null,SysRole::getId,id)
        );
        if(nameCount>0){
            throw new ProjectException(RoleExceptionEnum.ROLENAME_EXIST);
        }
        Long codeCount=sysRoleMapper.selectCount(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleCode,roleCode)
                .eq(SysRole::getDelFlag, CommonConstants.ZERO)
                .ne(id!=null,SysRole::getId,id)
        );
        if(codeCount>0){
            throw new ProjectException(RoleExceptionEnum.ROLECODE_EXIST);
        }
        //1.保存角色
        SysRole role=new SysRole();
        role.setId(paramReq.getId());
        role.setRoleName(roleName);
        role.setRoleCode(roleCode);
        role.setSort(paramReq.getSort());
        this.saveOrUpdate(role);

        Long roleId=role.getId();

        //2.清除菜单权限
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getSysRoleId,roleId));

        //3.保存菜单和按钮
        if(CollUtil.isNotEmpty(menuIds)){
            menuIds.forEach(menuId->{
                SysRoleMenu roleMenu=new SysRoleMenu();
                roleMenu.setSysRoleId(roleId);
                roleMenu.setSysMenuId(menuId);
                sysRoleMenuMapper.insert(roleMenu);
            });
        }
    }

    @Override
    public List<SysRole> listRole(RoleListReq param) {
        return sysRoleMapper.listRole(param);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        //删除角色
        sysRoleMapper.deleteById(id);
        //删除角色关联的菜单
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getSysRoleId,id));
    }
}
